Goals
- Learn the basics of the R programming language
- Learn the basics of Markdown writing
- Learn how to combine R code and markdown text to create RMarkdown documents
Outcomes
- TBD
- Create RMarkdown summary document of things we learned today!
07/03/2020
p.s. Allison Horst’s R illustrations are amaaaazing and I will be using them throughout.The funniest thing about this is that the time scale could be one day or the past 5 years.
— C.B. Standelmore (@bankingonbardo) December 4, 2019
We’re going to make this today!
Everything in R is an “object”
Variable: a symbol that stores/represents some other value or set of values.
Think of variables as containers.
Variables get assigned their value in R with a left arrow <-
x <- 5
This means the value of the variable we have named x is equal to 5.
String variables represent “strings” of text. Text has to be enclosed in quotes ""
my_var <- “hello”
my_var is equal to the text “hello”.In R, it’s pretty flexible. Not as flexible in other languages (*cough* Praat *cough*)
Some suggestions
Try your best to…
General rules:
| Not allowed | Why |
|---|---|
| 1abc | Starts with a number |
| intelligibility% | Contains a special character like !@#$%^&*, etc. |
| .1my_var | Starts with a period followed by a number (.1 |
| _my_var | Starts with an underscore (_) |
| special characters | Some words have special meaning to R and you shouldn’t overwrite them (e.g., “mean”) |
| Allowed | Good because | Bad because |
|---|---|---|
| x | short, lowercase | Not meaningful |
| intelligibility_scores | meaningful, easy to remember | Takes a long time to type |
| int_mean | meaningful, short | 🤷 |
| int_mean_pd, int_mean_hc | meaningful, shortish, descriptive | A bit long |
In your console, do the following:
In your console, do the following:
a and assign it a value of some text (don’t forget to enclose the text in quotes!)b and assign it a value of some other text# Set x equal to 0 x <- 0 # Now add 1 to the value of x y <- x+1
Packages are bundles of code written to do (typically) specific sets of functions
install.packages().install.packages("tidyverse")
install_github() which is part of the devtools package.# install.packages("devtools")
devtools::install_github("hadley/emo")
For example: To install the emo package from Hadley Wickham’s Github page:
library() function.# load the tidyverse package # expect a bunch of output messages (this is normal) library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3 ## ✓ tibble 2.1.3 ✓ dplyr 0.8.3 ## ✓ tidyr 0.8.3 ✓ stringr 1.4.0 ## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag()
# Typo! what happens? R yells at you. library(tidverse)
## Error in library(tidverse): there is no package called 'tidverse'
Functions: A certain named format of code that outlines a procedure. Often this allows several lines of code to be executed with a single line of code (by using the name of the function)
“The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.” www.tidyverse.org
Open 1_prep_data.R from today’s materials.
The goal of today’s workshop is to establish a workflow for using the rmarkdown package to “knit” together R code and text to create summary documents.
It might seem kind of odd to launch into a workflow that ties together R code and other stuff before we’ve actually had a chance to learn any R code, but here’s the rationale:
Rmd) to log the skills we worked on and any notes you’d like to keep for yourself. In this way, this skill is foundational.Markdown refers to a set of conventions for editing plain text. With markdown syntax, you write as you normally would in a text editor or word processor, but you signal text formatting with certain characters. Markdown (which is distinguished from markUP language) is designed to be easily readable, easy to write, and easy to learn.
*italic* **bold** **italic and bold!*** # First level header ## Second level header ## Third level header 1. the first item on a numbered list 2. the second item on a numbered list - the first item on a bulleted list - the second item on a bulleted list - item 2a Tables look like this: First Header | Second Header ------------- | ------------- Content Cell | Content Cell Content Cell | Content Cell